#include "td1.hpp"
#include "td2.hpp"
#include "td3.hpp"
#include "td4.hpp"
#include "bmp.hpp"


using namespace std;

int main()
{

	cout << "Start" <<  endl;

	unsigned int td = 5;
	FILE* file = NULL;

	switch(td){

	case 1 :{

		const unsigned int SIZE = 4, SIZE_FILTRE = 3, SIZE_RAMPE = 256, SIZE_LELECCUM = 4096;
		double* signal1 = new double [SIZE]();
		double* signal2 = new double [SIZE*2]();
		double* _h = new double [SIZE_FILTRE]();
		double* x_rampe = new double[SIZE_RAMPE];
		double* x_leleccum = new double[SIZE_LELECCUM];
		/*_h[0] = 1/sqrt(2);
		_h[1] = -1/sqrt(2);
		_h[2] = 0.0;

		signal1[0] = 2;signal1[1] = 2;signal1[2] = 2; signal1[3] = 2; signal2[0] = 0;
		signal2[1] = 1; signal2[2] = 2; signal2[3] = 3; signal2[4] = 4; signal2[5] = 5; signal2[6] = 6; signal2[7] = 7;
		 */

		/*
		 * EXO 1
		 */
		//interpolation_facteur2(signal1, signal2, SIZE);

		/*
		 * EXO 2
		 */

		//decimation_facteur2(signal2, signal1, SIZE*2);

		/*
		 * EXO 3
		 */

		//convolution(x, _h, SIZE_X, SIZE_FILTRE);

		/*
		 * EXO 4
		 */

		//Construction du signal x_rampe

		for(unsigned int i = 0; i<256; i++ ) x_rampe[i] = i;

		file = fopen("coef_xbd_xhd_rampe_haar.txt", "w");
		analyse_haar(x_rampe, SIZE_RAMPE, file);
		fclose(file);

		file = fopen("coef_signal_leleccum.txt", "r");
		chargeCoef(x_leleccum, SIZE_LELECCUM, file);
		fclose(file);

		file = fopen("coef_xbd_xhd_leleccum_haar.txt", "w");
		analyse_haar(x_leleccum, SIZE_LELECCUM, file);
		fclose(file);

		/*
		 * EXO 5
		 *
		 * /!\ Les coefficient xhd et xbd doivent avoir ete calcule via analuse_haar pour utiliser synthese_haar
		 */

		double* &xbd_xhd_rampe = x_rampe;	//On part des coefficients calcules grace a analyse_haar
		file = fopen("signal_rampe_reconstruit_haar.txt", "w");
		synthese_haar(xbd_xhd_rampe, SIZE_RAMPE, file);
		fclose(file);

		double* &xbd_xhd_leleccum = x_leleccum;	 //On part des coefficients calcules grace a analyse_haar
		file = fopen("signal_leleccum_reconstruit_haar.txt", "w");
		synthese_haar(xbd_xhd_leleccum, SIZE_LELECCUM, file);
		fclose(file);

		/*
		 *Exo 6
		 */
		for(unsigned int i = 0; i<256; i++ ) x_rampe[i] = i;

		file = fopen("coef_xbd_xhd_rampe_97.txt", "w");
		analyse_97(x_rampe, SIZE_RAMPE, file);
		fclose(file);

		file = fopen("coef_signal_leleccum.txt", "r");
		chargeCoef(x_leleccum, SIZE_LELECCUM, file);
		fclose(file);

		file = fopen("coef_xbd_xhd_leleccum_97.txt", "w");
		analyse_97(x_leleccum, SIZE_LELECCUM, file);
		fclose(file);


		file = fopen("signal_rampe_reconstruit_97.txt", "w");
		synthese_97(xbd_xhd_rampe, SIZE_RAMPE, file);
		fclose(file);

		file = fopen("signal_leleccum_reconstruit_97.txt", "w");
		synthese_97(xbd_xhd_leleccum, SIZE_LELECCUM, file);
		fclose(file);

		delete[] signal1;
		delete[] signal2;
		delete[] _h;
		delete[] x_rampe;
		delete[] x_leleccum;

		break;
	}
	case 2 :{
		cout << "td 2" <<  endl;
		unsigned int p = 4096;
		double * x = new double[p];

		file = fopen("leleccum.txt", "r");
		chargeCoef(x, p, file);
		fclose(file);

		//Exo 1

		file = fopen("coef_xbd_xhd_leleccum_lifting_97.txt", "w");
		analyse_97_lifting(x, p, file);
		fclose(file);

		//Exo 2

		double* &x_coef = x;
		file = fopen("signal_leleccum_reconstruit_lifting_97.txt", "w");
		synthese_97_lifting(x_coef, p, file);
		fclose(file);

		//Exo 3
		p = 512;
		double * test = new double[p];

		file = fopen("test_td2.txt", "r");
		chargeCoef(test, p, file);
		fclose(file);

		file = fopen("coef_xbd_xhd_test_haar.txt", "w");
		analyse_haar(test, p, file);
		fclose(file);

		file = fopen("coef_xbd_xhd_test_97.txt", "w");
		analyse_97(test, p, file);
		fclose(file);

		file = fopen("coef_xbd_xhd_test_lifting_97.txt", "w");
		analyse_97_lifting(test, p, file);
		fclose(file);

		delete[] test;
		delete[] x;

		break;
	}

	case 3 :{

		unsigned int p = 512, niveau = 2;
		double * x = new double[p];
		//x[0] = 5 ;x[1] = 1;x[2] = 2;x[3] = 2;x[4] = 2;x[5] = 2;x[6] = 2;x[7] = 1;

		file = fopen("test_td2.txt", "r");
		chargeCoef(x, p, file);
		fclose(file);

		file = fopen("coef_xbd_xhd_test_AMR_niveau_2.txt", "w");
		amr(x, p, niveau, file);
		fclose(file);
		file = fopen("signal_test_reconstruit_AMR_niveau_2.txt", "w");
		iamr(x, p, niveau, file);
		fclose(file);
		AMRStatistiques(x, p, niveau);

		niveau = 4;
		file = fopen("coef_xbd_xhd_test_AMR_niveau_4.txt", "w");
		amr(x, p, niveau, file);
		fclose(file);
		file = fopen("signal_test_reconstruit_AMR_niveau_4.txt", "w");
		iamr(x, p, niveau, file);
		fclose(file);
		AMRStatistiques(x, p, niveau);

		niveau = 8;
		file = fopen("coef_xbd_xhd_test_AMR_niveau_8.txt", "w");
		amr(x, p, niveau, file);
		fclose(file);
		file = fopen("signal_test_reconstruit_AMR_niveau_8.txt", "w");
		iamr(x, p, niveau, file);
		fclose(file);

		// EXO 3
		int u = 512, v = 512;
		double * m = charge_bmp256("lena.bmp", &u, &v);

		analyse2D_97(m, 512*512);
		ecrit_bmp256("coef_lena2", u, v, m);

		//synthese2D_97(m, 512*512);
		//ecrit_bmp256("synthese2D2_lena", u, v, m);
		delete[] x;

		break;
	}
	case 4:{

		int u = 512, v = 512;
		double * m = charge_bmp256("lena.bmp", &u, &v);
		int niveau = 3;
		std::vector<double>* varList = new std::vector<double>();

		amr2D_97(m ,u*v , niveau);

		for(int i = 1; i <= niveau; i++){
			for(int q = 1; q <= 3; q++){
				statSousBande(m, u*v, q, i, varList);
			}
		}
		statSousBande(m, u*v, 0, 3, varList);//Ajoute la bande coef d'approximation

		//ajusterCoef(m, u*v, niveau);

		iamr2D_97(m, u*v, niveau);


		//Exo 3
		//Calcul des debits binaires pour chaque sous bande
		for(int i = 0; i< varList->size(); i++) calculDebit(varList,i, 0.25);

		delete[] m;
		break;
	}
	case 5:{
		break;
	}
	default:
		break;


	}//end switch
	cout << "Exit" <<  endl;
	return 0;
}
